package com.ejuornal.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.bean.Page;
import com.ejuornal.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 项目名称：ejuornal 类名称：BaseAction 类描述：action层基类 创建人：陈晨 创建时间：Jan 19, 2013 2:17:48
 * PM 修改人：陈晨 修改时间：Jan 19, 2013 2:17:48 PM 修改备注：
 * 
 * @version
 */
@SuppressWarnings("serial")
public abstract class BaseAction<T> extends ActionSupport {
    protected int page = 1;
    protected Page<T> pages;
    protected String deleteIds;
    protected String order;
    protected Map<String, Object> dataMap = new HashMap<String, Object>();
    protected String prePage;
    protected String preAction;
    /**
	 * Title:getSession Description:获取session对象
	 * @return Map<String,Object>
	 */
	public Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}
	
	public HttpSession getSession2() {
	    return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * Title:getRequest Description:获取request
	 * 
	 * @return HttpServletRequest
	 */
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Title:getRequestParameter Description:获取request参数
	 * 
	 * @param parameter
	 * @return Object
	 */
	protected Object getRequestParameter(String parameter) {
		return ServletActionContext.getRequest().getParameter(parameter);
	}

	/**
	 * Title:setRequestAttribute Description:设置request属性值
	 * 
	 * @param name
	 * @param value
	 *            void
	 */
	public void setRequestAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	/**
	 * Title:getApplication Description:获取appliaction
	 * 
	 * @return ServletContext
	 */
	public static ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}
	
	public String getURl() {
	    HttpServletRequest request = this.getRequest();
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        return basePath;
    }
	
	public int getUserIdBySession() {
	    Integer user_id_1 = (Integer) ServletActionContext.getRequest().getSession().getAttribute(Constants.USERSSSIONID);
	    if(user_id_1 == null) {
	    	return -1;
	    }
	    return user_id_1;
	}
	public int getAdminIdBySession() {
	    Integer admin_id_1 = (Integer) ServletActionContext.getRequest().getSession().getAttribute(Constants.ADMIMID);
	    if(admin_id_1 == null) {
	    	return -1;
	    }
	    return admin_id_1;
	}
	
	public String getUserNameBySession() {
		String user_name = (String) ServletActionContext.getRequest().getSession().getAttribute(Constants.USERSESSION);
		if(user_name == null || "".equals(user_name)) {
			return null;
		}
		return user_name;
	}
	
	public String getUserEmailBySession() {
		String user_email = (String) ServletActionContext.getRequest().getSession().getAttribute(Constants.USERSESSIONEMAIL);
		
		if(user_email == null || "".equals(user_email)) {
			return null;
		}
		
		return user_email;
	}
	
	/**
	 * 接受String 类型id的参数id的实际类型是int
	 * @return
	 */
	public int getRequestParameter() {
		String id = (String) getRequest().getParameter("id");
		if(id == null || "".equals(id)) {
			return -1;
		}
		return Integer.parseInt(id);
	}
	
	public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public Page<T> getPages() {
        return pages;
    }

    public void setPages(Page<T> pages) {
        this.pages = pages;
    }
    
    public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getDeleteIds() {
        return deleteIds;
    }

    public void setDeleteIds(String deleteIds) {
        this.deleteIds = deleteIds;
    }
    
    public String getPrePage() {
        return prePage;
    }

    public void setPrePage(String prePage) {
        this.prePage = prePage;
    }

    public String getPreAction() {
        return preAction;
    }

    public void setPreAction(String preAction) {
        this.preAction = preAction;
    }
}
